package emp;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import common.interceptor.UserInterceptor;
import common.model.Department;
import common.model.User;
import login.LoginService;

import java.util.Date;
import java.util.List;

@Before(UserInterceptor.class)
public class EmpController extends Controller {

    private static final EmpService srv = EmpService.me;

    public void index() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        List<Department> departments = srv.getDepartmentByUser(user);
//        }
        if (!departments.isEmpty()) {
            setAttr("department", departments);
        }
        render("index.html");
    }

    public void getEmp() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));

        int department = getParaToInt("department");
        int search = getParaToInt("search");
        if (search != 0) {
            renderJson(srv.getSearchUserStringByUserAndDepartment(user, department, search));
        } else {
            renderJson(srv.getUserStringByUserAndDepartment(user, department));
        }
    }

    public void getWorkEmp() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));

        int department = getParaToInt("department");
        int search = getParaToInt("search");
        if (search != 0) {
            renderJson(srv.getSearchWorkUserStringByUserAndDepartment(user, department, search));
        } else {
            renderJson(srv.getWorkUserStringByUserAndDepartment(user, department));
        }
    }


    public void department() {
        render("department.html");
    }

    public void getDepartment() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        int search = getParaToInt("search");
        if (search != 0) {
            renderJson(srv.getStringDepartmentStringByUser(user, search));
        } else {
            renderJson(srv.getDepartmentStringByUser(user));
        }
    }

    public void transfer() {

        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        List<Department> departments = srv.getDepartmentByUser(user);
//        for (Department department:departments){
//            System.out.println(department.getDName());
//        }
        if (!departments.isEmpty()) {
            setAttr("department", departments);
        }
        render("transfer.html");
    }

    public void entry() {
        render("entry.html");
    }

    public void getEntry() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        int search = getParaToInt("search");
        if (search != 0) {
            renderJson(srv.getSearchEntryStringByUser(user, search));
        } else {
            renderJson(srv.getEntryStringByUser(user));
        }
    }

    public void dimission() {
        render("dimission.html");
    }

    public void getDimission() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        String status = getPara("status");
        int me = getParaToInt("me");
        if (me == 1) {
            status = "全部";
        }

        renderJson(srv.getDimissionStringByUserAndStatus(user, status));


    }

    public void newEmp() {
        String username = getPara("username");
        String pwd = getPara("pwd");
        String gender = getPara("gender");
        Date birthday = getDate("birthday");
        Date recordTime = new Date();
        String idCard = getPara("idCard");
        String area = getPara("area");
        String nativePlace = getPara("nativePlace");
        String state = getPara("state");
        int department = getParaToInt("department");
        String level = getPara("level");
        String Sadmin = getPara("admin");
        boolean admin = true;
        if (Sadmin == null) {
            admin = false;
        }
        renderJson(srv.newEmp(username, pwd, gender, birthday, recordTime, idCard, area, nativePlace, state, department, level, admin));
    }

    public void newDepartment() {
        String dName = getPara("dName");
        String mission = getPara("mission");
        String remarks = getPara("remarks");
        int manager = getParaToInt("manager");


        renderJson(srv.newDepartment(dName, mission, remarks, manager));
    }

    public void editDepartment() {
        int id = getParaToInt("id");
        String mission = getPara("mission");
        String remarks = getPara("remarks");

        renderJson(srv.editDepartment(id, mission, remarks));
    }

    public void newDimission() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        String reason = getPara("reason");

        renderJson(srv.newDimission(reason, user));
    }

    public void deleteDepartment() {
        int id = getParaToInt("id");
        renderJson(srv.deleteDepartment(id));
    }

    public void newFire() {
        int id = getParaToInt("id");
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        String reason = getPara("reason");
        renderJson(srv.newFire(id, user, reason));
    }

    public void agreeDimission() {
        int id = getParaToInt("id");
        renderJson(srv.agreeDimission(id));
    }

    public void refuseDimission() {
        int id = getParaToInt("id");
        renderJson(srv.refuseDimission(id));
    }

    public void deleteDimission() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        int id = getParaToInt("id");
        renderJson(srv.deleteDimission(id, user));
    }

    public void getTransfer() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        int search = getParaToInt("search");
        if (search != 0) {
            renderJson(srv.getSearchTranserStringByUser(user, search));
        } else {
            renderJson(srv.getTransferStringByUser(user));
        }
    }

    public void newTransfer() {
        int emp = getParaToInt("emp");
        String state = getPara("state");
        int department = getParaToInt("department");
        String level = getPara("level");
        String Sadmin = getPara("admin");
        String admin = "是";
        boolean badmin = true;
        if (Sadmin == null) {
            admin = "否";
            badmin = false;
        }
        renderJson(srv.newTransfer(emp, state, department, level, admin, badmin));
    }
}
